<?php
session_start();
require_once '../config.php';
require_once '../functions.php';

// 检查管理员是否登录
if (!is_admin_logged_in()) {
    header('Location: login.php');
    exit;
}

$message = '';
$message_type = '';

// 手动添加封禁IP
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ban_ip'])) {
    $ip = trim($_POST['ip'] ?? '');
    $reason = trim($_POST['reason'] ?? '');
    
    if (empty($ip)) {
        $message = 'IP地址不能为空';
        $message_type = 'error';
    } else {
        // 简单验证IP格式
        if (!filter_var($ip, FILTER_VALIDATE_IP)) {
            $message = 'IP地址格式不正确';
            $message_type = 'error';
        } else {
            try {
                // 检查是否已被封禁
                $stmt = $pdo->prepare("SELECT id FROM banned_ips WHERE ip_address = ?");
                $stmt->execute([$ip]);
                
                if ($stmt->rowCount() > 0) {
                    $message = '该IP已在封禁列表中';
                    $message_type = 'info';
                } else {
                    $stmt = $pdo->prepare("INSERT INTO banned_ips (ip_address, reason, banned_by) VALUES (?, ?, ?)");
                    $stmt->execute([$ip, $reason, $_SESSION['admin_id']]);
                    $message = 'IP封禁成功';
                    $message_type = 'success';
                }
            } catch (PDOException $e) {
                $message = '操作失败，请重试';
                $message_type = 'error';
            }
        }
    }
}

// 解除封禁
if (isset($_GET['action']) && $_GET['action'] === 'unban' && isset($_GET['id'])) {
    $id = $_GET['id'];
    if (is_numeric($id)) {
        $id = (int)$id;
        try {
            $stmt = $pdo->prepare("DELETE FROM banned_ips WHERE id = ?");
            $stmt->execute([$id]);
            $message = '已解除封禁';
            $message_type = 'success';
        } catch (PDOException $e) {
            $message = '操作失败，请重试';
            $message_type = 'error';
        }
    } else {
        $message = '参数错误';
        $message_type = 'error';
    }
}

// 搜索功能
$search = $_GET['search'] ?? '';
if (!empty($search)) {
    $searchTerm = "%{$search}%";
    $stmt = $pdo->prepare("SELECT b.*, a.username FROM banned_ips b LEFT JOIN admins a ON b.banned_by = a.id WHERE b.ip_address LIKE ? OR b.reason LIKE ? ORDER BY b.banned_at DESC");
    $stmt->execute([$searchTerm, $searchTerm]);
} else {
    // 获取所有封禁IP
    $stmt = $pdo->query("SELECT b.*, a.username FROM banned_ips b LEFT JOIN admins a ON b.banned_by = a.id ORDER BY b.banned_at DESC");
}
$banned_ips = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>封禁管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/all.min.css">
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Arial', sans-serif;
        }
        a {
            text-decoration: none;
            outline: none;
        }
        .sidebar {
            width: 250px; /* 固定宽度 */
            height: 100vh;
            background-color: #ff8fab;
            color: white;
            padding-top: 20px;
            position: fixed;
            overflow-y: auto;
            transition: transform 0.3s ease; /* 添加过渡效果 */
            z-index: 100; /* 确保在内容上方 */
        }
        
        .sidebar-header {
            text-align: center;
            padding-bottom: 20px;
            border-bottom: 1px solid rgba(255,255,255,0.2);
            margin-bottom: 20px;
        }
        
        .sidebar-header h2 {
            margin: 0;
            font-size: 1.5rem;
        }
        
        .nav-link {
            color: white;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s;
            border-left: 3px solid transparent;
        }
        
        .nav-link:hover, .nav-link.active {
            background-color: rgba(255,255,255,0.1);
            color: white;
            text-decoration: none;
            border-left-color: #ffd700;
        }
        
        .main-content {
            margin-left: 250px;
            padding: 30px;
            transition: margin-left 0.3s ease; /* 添加过渡效果 */
            min-height: 100vh;
        }
        
        .page-title {
            color: #d63384;
            margin-bottom: 30px;
            font-size: 1.8rem;
        }
        
        .card {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            padding: 20px;
            margin-bottom: 30px;
        }
        
        .card-title {
            color: #d63384;
            margin-top: 0;
            margin-bottom: 20px;
            font-size: 1.3rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-control {
            width: 100%;
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1rem;
        }
        
        .form-control:focus {
            border-color: #ff69b4;
            outline: none;
        }
        
        textarea.form-control {
            min-height: 100px;
            resize: vertical;
        }
        
        .btn {
            padding: 10px 20px;
            border-radius: 5px;
            border: none;
            cursor: pointer;
            font-size: 1rem;
            transition: background-color 0.3s;
        }
        
        .btn-primary {
            background-color: #ff69b4;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #d63384;
        }
        
        .btn-success {
            background-color: #28a745;
            color: white;
        }
        
        .btn-success:hover {
            background-color: #218838;
        }
        
        .table-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            overflow: hidden;
        }
        
        .data-table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .data-table th, .data-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }
        
        .data-table th {
            background-color: #fff0f3;
            color: #d63384;
            font-weight: bold;
        }
        
        .data-table tr:hover {
            background-color: #f9f9f9;
        }
        
        .action-btn {
            padding: 5px 10px;
            border-radius: 5px;
            border: none;
            cursor: pointer;
            font-size: 0.9rem;
            transition: background-color 0.3s;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-unban {
            background-color: #28a745;
            color: white;
        }
        
        .btn-unban:hover {
            background-color: #218838;
            color: white;
        }
        
        .message {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        
        .message-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .message-info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        
        .search-box {
            text-align: right;
            margin-bottom: 15px;
        }
        
        .search-form {
            display: inline-flex;
            gap: 5px;
        }
        
        .search-input {
            padding: 8px 15px;
            border-radius: 20px;
            border: 1px solid #ddd;
            width: 250px;
        }
        
        .search-btn {
            padding: 8px 15px;
            border-radius: 20px;
            border: none;
            background-color: #ff69b4;
            color: white;
            cursor: pointer;
        }
        
        .logout-btn {
            margin-top: 20px;
            text-align: center;
        }
        
        .logout-btn a {
            color: white;
            text-decoration: none;
            display: block;
            padding: 10px;
            background-color: rgba(255,255,255,0.1);
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        
        .logout-btn a:hover {
            background-color: rgba(255,255,255,0.2);
        }
        
        @media (max-width: 768px) {
            .sidebar {
                width: 100%;
                height: auto;
                position: relative;
            }
            
            .main-content {
                margin-left: 0;
                padding: 20px;
            }
            
            .search-box {
                text-align: left;
            }
            
            .search-form {
                width: 100%;
            }
            
            .search-input {
                width: 100%;
            }
            
            .data-table th:nth-child(3), 
            .data-table td:nth-child(3),
            .data-table th:nth-child(4), 
            .data-table td:nth-child(4) {
                display: none;
            }
        }
    </style>
</head>
<body>
    <div class="sidebar col-md-3 col-lg-2 d-md-block">
        <div class="sidebar-header">
            <h2><?php echo SITE_NAME; ?> 管理后台</h2>
        </div>
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link" href="index.php">
                    <i class="fas fa-tachometer-alt"></i> 仪表盘
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="confessions.php">
                    <i class="fas fa-heart"></i> 表白管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="dailies.php">
                    <i class="fas fa-coffee"></i> 日常管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="announcements.php">
                    <i class="fas fa-bullhorn"></i> 公告管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="comments.php">
                    <i class="fas fa-comments"></i> 评论管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="prohibited_words.php">
                    <i class="fas fa-ban"></i> 违禁词管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link active" href="banned_ips.php">
                    <i class="fas fa-shield-alt"></i> 封禁管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="settings.php">
                    <i class="fas fa-cog"></i> 系统设置
                </a>
            </li>
        </ul>
        
        <div class="logout-btn">
            <a href="logout.php">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </a>
        </div>
    </div>
    
    <main class="main-content">
        <h1 class="page-title"><i class="fas fa-shield-alt"></i> 封禁管理</h1>
        
        <?php if (!empty($message)): ?>
            <div class="message message-<?php echo $message_type; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        
        <div class="card">
            <h2 class="card-title"><i class="fas fa-plus-circle"></i> 手动封禁IP</h2>
            <form method="post" action="banned_ips.php">
                <div class="form-group">
                    <label for="ip">IP地址：</label>
                    <input type="text" id="ip" name="ip" class="form-control" placeholder="输入要封禁的IP地址">
                </div>
                <div class="form-group">
                    <label for="reason">封禁原因：</label>
                    <textarea id="reason" name="reason" class="form-control" placeholder="请输入封禁原因"></textarea>
                </div>
                <button type="submit" name="ban_ip" class="btn btn-primary"><i class="fas fa-ban"></i> 执行封禁</button>
            </form>
        </div>
        
        <div class="card">
            <h2 class="card-title"><i class="fas fa-list"></i> 封禁列表</h2>
            
            <div class="search-box">
                <form method="get" action="banned_ips.php" class="search-form">
                    <input type="text" name="search" class="search-input" placeholder="搜索IP或原因..." value="<?php echo htmlspecialchars($search); ?>">
                    <button type="submit" class="search-btn"><i class="fas fa-search"></i></button>
                </form>
            </div>
            
            <div class="table-container">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>IP地址</th>
                            <th>封禁原因</th>
                            <th>操作管理员</th>
                            <th>封禁时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (count($banned_ips) > 0): ?>
                            <?php foreach ($banned_ips as $ban): ?>
                                <tr>
                                    <td><?php echo $ban['id']; ?></td>
                                    <td><?php echo $ban['ip_address']; ?></td>
                                    <td><?php echo htmlspecialchars($ban['reason']); ?></td>
                                    <td><?php echo $ban['username'] ?? '未知'; ?></td>
                                    <td><?php echo date('Y-m-d H:i', strtotime($ban['banned_at'])); ?></td>
                                    <td>
                                        <a href="banned_ips.php?action=unban&id=<?php echo $ban['id']; ?><?php echo !empty($search) ? "&search=" . urlencode($search) : ""; ?>" class="action-btn btn-unban" onclick="return confirm('确定要解除对该IP的封禁吗？')">
                                            <i class="fas fa-check"></i> 解除封禁
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="6" style="text-align: center; padding: 30px;">
                                    暂无封禁记录
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </main>
</body>
</html>
